function getFormData($form){
    $form.submit(function( event ) {
      console.log( $( this ).serializeArray() );
      event.preventDefault();
    });
    var unindexed_array = $form.serializeArray();
    var indexed_array = {};

    $.map(unindexed_array, function(n, i){
        indexed_array[n['name']] = n['value'];
    });

    return indexed_array;
}

function initFormValue($form,values){
    console.log("initFormValue")
    $.each(values,function(key,value){
        var ctrl = $('[name='+key+']', $form);
        switch(ctrl.prop("type")) {
            case "radio": case "checkbox":
                ctrl.each(function() {
                    if($(this).attr('value') == value) $(this).attr("checked",value);
                });
                break;
            default:
                ctrl.val(value);
        }
    })
}



function redirect(url){
    window.location = url
}

function myFileUpload(url,$fileInput,success){
    var formData = new FormData();
    formData.append('file', $fileInput[0].files[0]);
    $.ajax({
       url : url,
       type : 'POST',
       data : formData,
       processData: false,  // tell jQuery not to process the data
       contentType: false,  // tell jQuery not to set contentType
       success : success
    });
}

function classSelect($select,success){
    console.log("classSelect")
    listMyClass(function(res){
        var options = ['<option>请选择班级</option>']
        if(res.data){
            $.each(res.data,function(index,element){
                options.push('<option value="'+element.id+'">'+element.name+'</option>')
            })
        }
        $select.empty().append(options.join(""))
        if (typeof success === "function") {
            success()
        }
    })
}

function teacherOptions($select,success){
    console.log("teacherOptions")
    listUser({userType:2},function(res){
        var options = ['<option value="0">请选择教师</option>']
        if(res.data){
            $.each(res.data,function(index,element){
                options.push('<option value="'+element.id+'">'+element.realName+'</option>')
            })
        }
        $select.empty().append(options.join(""))
        if (typeof success === "function") {
            success()
        }
    })
}

function internshipProgramOptions($select,success){
    console.log("internshipProgramOptions")
    listInternshipProgram(function(res){
        var options = ['<option value="0">请选择实习计划</option>']
        if(res.data){
            $.each(res.data,function(index,element){
                options.push('<option value="'+element.id+'">'+element.name+'</option>')
            })
        }
        $select.empty().append(options.join(""))
        if (typeof success === "function") {
            success()
        }
    })
}

function studentOptions($select,data,success){
    console.log("studentOptions")
    listUser({userType:3,...data},function(res){
        var options = ['<option value="0">请选择学生</option>']
        if(res.data){
            $.each(res.data,function(index,element){
                options.push('<option value="'+element.id+'">'+element.realName+'</option>')
            })
        }
        $select.empty().append(options.join(""))
        if (typeof success === "function") {
            console.log("studentOptions.success")
            success()
        }
    })
}

